using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using SamasDBI;
using SamasDBI.MySQL;
using SamasUtilities;

namespace EQEmuMysql
{
    public class Variables
    {
        private string sVarName;
        private string sValue;
        private string sInformation;
        private string sTS;

        private bool flgIsNew = false;
        private bool flgDeleted = false;

        private static int nMAXID = 0;

        public string VarName
        {
            get { return sVarName; }
            set { sVarName = value; }
        }
        public string Value
        {
            get { return sValue; }
            set { sValue = value; }
        }
        public string Information
        {
            get { return sInformation; }
            set { sInformation = value; }
        }
        public string TS
        {
            get { return sTS; }
            set { sTS = value; }
        }        

        public bool IsNew
        {
            get { return flgIsNew; }
        }


        public Variables()
        {
            

            sVarName = "";
            sValue = "";
            sInformation = "";
            sTS = "";


            flgIsNew = true;
        }

        public Variables(string VarName)
        {
            MySqlConnection conGet = new MySqlConnection(MySQLRunner.ConnectionString);
            MySqlDataReader drGet = null;
            string sSQL = "";

            sSQL = "SELECT * FROM variables WHERE varname='" + VarName + "'";

            if (MySQLRunner.ExecuteQuery(sSQL, conGet, ref drGet) == true)
            {
                drGet.Read();

                if (drGet.HasRows == false)
                {                    

                    sVarName = "";
                    sValue = "";
                    sInformation = "";
                    sTS = "";                    

                    flgIsNew = true;
                }
                else
                {
                    sVarName = drGet.GetString(drGet.GetOrdinal("varname"));
                    sValue = drGet.GetString(drGet.GetOrdinal("value"));
                    sInformation = drGet.GetString(drGet.GetOrdinal("information"));
                    //sTS = drGet.GetString(drGet.GetOrdinal("ts"));                    

                    flgIsNew = false;
                }
            }
            else
            {
                MessageBox.Show("Error connecting to the database.");
            }

            if (conGet.State == ConnectionState.Open)
            {
                if (drGet != null)
                    drGet.Close();

                conGet.Close();
            }
        }

        public void Delete()
        {
            flgDeleted = true;
        }

        public bool Save()
        {
            bool flgReturn;

            if (flgDeleted == false)
            {
                if (flgIsNew == true)
                    flgReturn = Add();
                else
                    flgReturn = Update();
            }
            else
            {
                flgReturn = DeleteObject();
            }

            if (flgReturn == true)
                flgIsNew = false;

            return flgReturn;
        }

        private bool Update()
        {
            MySqlConnection conUpdate = new MySqlConnection(MySQLRunner.ConnectionString);
            string sSQL = "";
            bool flgReturn = false;

            sSQL = "UPDATE variables SET " +
                   "varname='" + sVarName + "', " +
                   "value='" + sValue + "', " +
                   "information='" + sInformation + "' " +
                   "WHERE varname='" + sVarName + "'";

            if (MySQLRunner.ExecuteNonQuery(sSQL, conUpdate) == false)
                flgReturn = false;
            else
                flgReturn = true;

            if (conUpdate.State == ConnectionState.Open)
            {
                conUpdate.Close();
            }
            return flgReturn;
        }

        private bool Add()
        {
            MySqlConnection conAdd = new MySqlConnection(MySQLRunner.ConnectionString);
            string sSQL = "";
            bool flgReturn = false;

            sSQL = "";

            if (MySQLRunner.ExecuteNonQuery(sSQL, conAdd) == false)
                flgReturn = false;
            else
                flgReturn = true;

            if (conAdd.State == ConnectionState.Open)
            {
                conAdd.Close();
            }

            return flgReturn;
        }

        public bool DeleteObject()
        {
            MySqlConnection conDelete = new MySqlConnection(MySQLRunner.ConnectionString);
            string sSQL = "";
            bool flgReturn = false;

            sSQL = "DELETE FROM variables WHERE varname=" + sVarName;

            if (MySQLRunner.ExecuteNonQuery(sSQL, conDelete) == false)
                flgReturn = false;
            else
                flgReturn = true;

            if (conDelete.State == ConnectionState.Open)
            {
                conDelete.Close();
            }

            return flgReturn;
        }
    }
}
